<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item prop="projectId">
        <el-select v-model="queryParams.projectId" placeholder="请选择">
          <el-option-group
            v-for="dict in dict.type.ly_group"
            :key="dict.value"
            :label="dict.label"
          >
            <el-option
              v-for="item in getProjectOption(
                groupAndProjectOptions,
                dict.value
              )"
              :key="item.projectId"
              :label="item.projectName"
              :value="item.projectId"
            >
            </el-option>
          </el-option-group>
        </el-select>
      </el-form-item>
      <el-form-item prop="examinePlanTitle">
        <el-input
          v-model="queryParams.examinePlanTitle"
          placeholder="计划名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item prop="isUnusual">
        <el-select v-model="queryParams.isUnusual" placeholder="是否异常">
          <el-option :key="1" :label="'正常'" :value="0" />
          <el-option :key="0" :label="'异常'" :value="1" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-date-picker
          v-model="startToEndTime"
          type="daterange"
          range-separator="~"
          value-format="yyyy-MM-dd"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">
          搜索
        </el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery"> 重置 </el-button>
        <el-button @click="exportRecords" type="warning"> 导出记录 </el-button>
        <el-button @click="exportDetail" type="warning"> 导出详情 </el-button>
      </el-form-item>
    </el-form>

    <el-table
      v-loading="loading"
      :data="dataList"
      @selection-change="handleSelectionChange"
      :border="true"
    >
      <el-table-column type="selection" width="55"> </el-table-column>
      <el-table-column label="ID" align="center" prop="examineFlowId" />
      <el-table-column
        label="计划名称"
        align="center"
        prop="examinePlanTitle"
        :show-overflow-tooltip="true"
        width="200px"
      />
      <el-table-column
        label="计划类型"
        align="center"
        prop="type"
        :show-overflow-tooltip="true"
      >
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.ly_inspection_type"
            :value="scope.row.type"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="巡检区域"
        align="center"
        prop="tenantName"
        :show-overflow-tooltip="true"
        width="220"
      />
      <el-table-column label="状态" align="center" prop="examinePlanStatus">
        <template slot-scope="scope">
          <span
            :class="{
              ing: scope.row.examinePlanStatus === '1',
              start: scope.row.examinePlanStatus === '0',
            }"
          >
            {{
              scope.row.examinePlanStatus === "0"
                ? "待启动"
                : scope.row.examinePlanStatus === "1"
                ? "执行中"
                : scope.row.examinePlanStatus === "2"
                ? "已完成"
                : null
            }}
          </span>
        </template>
      </el-table-column>
      <el-table-column
        label="异常项"
        align="center"
        prop="isUnusual"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="异常项类型"
        align="center"
        prop="examineProjectTitle"
        width="220px"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
      <el-table-column
        label="巡检人员"
        align="center"
        prop="examineProjectType"
        width="220px"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
      <el-table-column
        label="巡检时间"
        align="center"
        prop="createTime"
        width="220px"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
      <el-table-column
        label="巡检备注"
        align="center"
        prop="examineFlowRemark"
        width="220px"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        fixed="right"
        width="120px"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleDetail(scope.row)"
            >详情</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.page"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 详情对话框 -->
    <el-dialog
      :fullscreen="true"
      :center="true"
      :visible.sync="open"
      append-to-body
    >
      <div class="dialog-content">
        <el-form ref="form" :model="form" size="medium" label-width="110px">
          <div class="top">
            <el-divider content-position="left">
              <span style="font-size: 20px">基本信息</span>
            </el-divider>
            <div class="top-content">
              <el-row :gutter="15">
                <el-col :span="8">
                  <el-form-item label="巡检计划名称" prop="examinePlanTitle">
                    <el-input
                      v-model="form.examinePlanTitle"
                      clearable
                      :readonly="true"
                    />
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="类型" prop="type">
                    <el-select v-model="form.type" :disabled="true">
                      <el-option
                        v-for="dict in dict.type.ly_inspection_type"
                        :key="dict.value"
                        :label="dict.label"
                        :value="dict.value"
                      />
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="巡检时间" prop="createTime">
                    <el-input v-model="form.createTime" :readonly="true" />
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row :gutter="15">
                <el-col :span="8">
                  <el-form-item label="巡检人：" prop="inspectors">
                    <el-input :readonly="true" v-model="form.inspectors" />
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="项目名称" prop="examinePlanTitle">
                    <el-input
                      v-model="form.examinePlanTitle"
                      :readonly="true"
                    />
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="巡检区域" prop="tenantName">
                    <el-input v-model="form.tenantName" :readonly="true" />
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row :gutter="15">
                <el-col :span="16">
                  <el-form-item label="巡检地点" prop="examineFlowAddress">
                    <el-input
                      v-model="form.examineFlowAddress"
                      :readonly="true"
                    />
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="陪同巡检人：" prop="inspectors">
                    <el-input v-model="form.inspectors" :readonly="true" />
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="24">
                  <el-form-item label="附件">
                    <el-image
                      style="width: 100px; height: 100px"
                      :src="form.examineFlowImgs"
                      :preview-src-list="[form.examineFlowImgs]"
                    >
                    </el-image>
                  </el-form-item>
                </el-col>
              </el-row>
            </div>
          </div>
          <div class="bottom">
            <el-divider content-position="left">
              <span style="font-size: 20px">巡检项</span>
            </el-divider>
            <div class="bottom-content">
              <ul class="inspection-item">
                <li v-for="item in form.examineItems">
                  <span>{{ item.examineProjectTitle }} </span>
                  <span
                    :class="{
                      error: item.selectValue !== '0',
                      normal: item.selectValue === '0',
                    }"
                    >{{ item.selectValue === "0" ? "正常" : "异常" }}</span
                  >
                  <div class="desc" v-if="item.flowProjectWords">
                    <span>描述：{{ item.flowProjectWords }}</span>
                  </div>
                  <div class="imgs" v-if="item.flowProjectImgs">
                    <el-image
                      v-for="item in item.flowProjectImgs.split(',')"
                      style="width: 100px; height: 100px"
                      :src="item"
                      :preview-src-list="item.flowProjectImgs.split(',')"
                    >
                    </el-image>
                  </div>
                </li>
              </ul>
            </div>
          </div>
        </el-form>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">返回</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listRecords, getFlow, getFlowApi } from "@/api/inspection/record";
import { getAllGroupAndProjectApi } from "@/api/inspection/config";

export default {
  name: "record",
  dicts: ["ly_inspection_type", "ly_group", "ly_examine_project_type"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      dataList: [],
      // 企业项目数据列表
      groupAndProjectOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        page: 1,
        pageSize: 10,
        projectId: "",
        examinePlanTitle: "",
        isUnusual: "",
        beginDate: "",
        endDate: "",
      },
      startToEndTime: [],
      // 表单参数
      form: {},
    };
  },
  created() {
    this.getList();
    this.getAllGroupAndProject();
  },
  methods: {
    /** 查询数据表 */
    getList() {
      this.loading = true;
      listRecords(this.queryParams).then((response) => {
        this.dataList = response.data.records;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    getFlowDetailById(id) {
      getFlowApi(id).then((res) => {
        this.form = res.data;
      });
    },
    getAllGroupAndProject() {
      getAllGroupAndProjectApi().then((response) => {
        this.groupAndProjectOptions = response.data;
      });
    },
    getProjectOption(groupAndProjectOptions, groupId) {
      const data = groupAndProjectOptions.filter(
        (option) => option.groupId === groupId
      );
      return data.length ? data[0].projectList : [];
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        examineConfigId: null,
        groupId: null,
        projectId: null,
        examineAlias: null,
        examineCateType: null,
        examineProjectTitle: null,
        examineProjectName: null,
        examineProjectType: null,
        defaultValue: null,
        isNeedWords: null,
        isNeedFile: null,
        dutyRemark: null,
        sort: null,
        createTime: null,
        deleteTime: null,
        status: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.page = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 详情
    handleDetail(row) {
      this.open = true;
      this.getFlowDetailById(row.examineFlowId);
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.examineConfigId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 导出记录 */
    exportRecords() {
      return;
    },
    /** 导出详情 */
    exportDetail() {
      return;
    },
  },
};
</script>

<style lang="scss" scoped>
.inspection-item {
  list-style-type: decimal;
  font-size: 16px;
  li {
    margin-bottom: 20px;
    .desc {
      margin: 10px 0;
    }
  }
}
.error {
  color: red;
}
.normal {
  color: #4284ff;
}
</style>
